package com.cskaoyan.service.vx.collect;

import com.cskaoyan.bean.bo.vxBo.collect.VXCollectAddOrDeleteBo;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.mapper.wxMapper.MarketCollectMapper;
import com.cskaoyan.mapper.wxMapper.MarketGoodsMapper;
import com.cskaoyan.mapper.wxMapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.Date;
import java.util.List;

/**
 *
 *  该实现类负责  collect service;
 *
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/9 14:19
 */
@Service
public class VXControllerServiceImpl implements VXCollectService {
        @Autowired
    MarketCollectMapper marketCollectMapper;
        @Autowired
    MarketGoodsMapper marketGoodsMapper;
        @Autowired
        MarketTopicMapper marketTopicMapper;
    //     维护内部
    // boolean flag ;



    @Override
    public TVO<CollectionListPo> queryCollectList(Integer id, Integer type, Integer page, Integer limit) {
        TVO tvo = new TVO();
        PageHelper.startPage(page, limit);
        if (type == 0) {
            List<CollectionListPo> collectionListPos = marketCollectMapper.queryCollectListByGoods(id);
            PageInfo pageInfo = new PageInfo(collectionListPos);
            tvo = TVO.data(pageInfo.getTotal(), pageInfo.getPages(), limit, page, collectionListPos);
        } else if(type == 1) {
            List<CollectionListPo> collectionListPos = marketCollectMapper.queryCollectListByTopic(id);
            PageInfo pageInfo = new PageInfo(collectionListPos);
            tvo = TVO.data(pageInfo.getTotal(), pageInfo.getPages(), limit, page, collectionListPos);
        }
        return tvo;
    }


    public void addordelete(VXCollectAddOrDeleteBo vxCollectAddOrDeleteBo) {
        // 读取 market_collect 里,
        // 先判断是否已经被收藏,
        //                  若 存在 user_id 和 value_id 和 type 和 deleted  ; 说明已被收藏;

        //                      没有查到,即 未被收藏;
        Integer valueId = vxCollectAddOrDeleteBo.getValueId();// 根据  type 去确定valueId到底是 topicId 还是 valueId;
        Integer type = vxCollectAddOrDeleteBo.getType();
        /**
         * 通过 shiro 来统一获取 Market_user 表的 userId;
         */
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo((byte) type.intValue()); // 必须 Integer -> int -> byte
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);// 查询出 deleted = 0 ;

        // 获取market_collect表里 符合  valueId= ? and userId = ?  and type =? and  deleted = ? 条件的信心
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        int size = marketCollects.size();

        if (size==0){
            // 说明尚未被收藏; 去 add;
            if (type ==0){
                // 是 goodsId, 即 market_goods 表;
                MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(vxCollectAddOrDeleteBo.getValueId());
                MarketCollect marketCollect = new MarketCollect();
                marketCollect.setValueId(vxCollectAddOrDeleteBo.getValueId());
                marketCollect.setType(((byte) vxCollectAddOrDeleteBo.getType().intValue()));// 必须 Integer-> int -> byte
                marketCollect.setAddTime(marketGoods.getAddTime()); //
                marketCollect.setUpdateTime(marketGoods.getUpdateTime());//
                marketCollect.setDeleted(false); // false 表示 0 ; 即 新增该收藏;
                marketCollect.setUserId(userId);
                int count = marketCollectMapper.insert(marketCollect);// 插入 market_collect表;
            }else{

                // type == 1 , 即为 typeId, 对应于 market_topic 表;
                MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(vxCollectAddOrDeleteBo.getValueId());
                MarketCollect marketCollect = new MarketCollect();
                marketCollect.setValueId(vxCollectAddOrDeleteBo.getValueId());
                marketCollect.setType(((byte) vxCollectAddOrDeleteBo.getType().intValue()));// 必须 Integer-> int -> byte
                marketCollect.setAddTime(marketTopic.getAddTime()); //
                marketCollect.setUpdateTime(marketTopic.getUpdateTime());//
                marketCollect.setDeleted(false); // false 表示 0 ; 即 新增该收藏;
                marketCollect.setUserId(userId);
                marketCollectMapper.insertSelective(marketCollect);// 插入 market_collect表;
            }
        }else{
            // 虽然返回一个 list; 但是符合要求的应该仅仅有
            MarketCollect marketCollect1 = marketCollects.get(0);
            Integer id = marketCollect1.getId();
            // 已被收藏;  去 delete;
            MarketCollectExample marketCollectExample1 = new MarketCollectExample();
            marketCollectExample1.createCriteria().andIdEqualTo(id);
            // Integer id = marketCollect1.getId();
            if (type==0)
            {
                marketCollect1.setDeleted(true);
                // 删除 goods; 假删除 // 参数 1: 一个 record,   参数 2: 条件marketCollectExample1
                marketCollectMapper.updateByExample(marketCollect1,marketCollectExample1);// where deleted =1;
                // marketCollectMapper.updateByExampleSelective()


            }else {
                // 删除 topic;假删除;
                marketCollect1.setDeleted(true);
                marketCollectMapper.updateByExampleSelective(marketCollect1,marketCollectExample1);

            }

        }



    }


}
